#人脸追踪模块
from machine import Pin,SPI
from ssd1351 import Display
import camera,time,image
spi = SPI(2, baudrate=14500000, sck=Pin(2), mosi=Pin(14))
display = Display(spi, dc=Pin(15), cs=Pin(13), rst=Pin(12),width=128,height=128)
camera.init(0,format=0)
face_cascade = image.HaarCascade("frontalface", stages=25)

s=time.ticks_ms()
while True:
    ea=camera.capture().resize(128,96)#性能过低 请使用屏幕分辨率进行，320x240 FPS为0.1FPS
    img=ea.copy().to_grayscale()
    objects = img.find_features(face_cascade, threshold=0.5, scale=1.25)
    if objects:
        # 在每个方向上将ROI扩大15个像素
        face = (objects[0][0]-15, objects[0][1]-15,objects[0][2]+15*2, objects[0][3]+15*2)
        # 使用检测面大小作为ROI提取关键点
        kpts1 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, roi=face)
        # 在第一个人脸周围画一个矩形
        ea.draw_rectangle(objects[0])
    fps=1000/(time.ticks_ms()-s)
    s=time.ticks_ms()
    ea.draw_string(10,80,str(fps)[0:3]+"fps",color=[255,2,25])
    display.draw_image_ram(ea.to_bytes(), 0, 30, 128, 96)
    
